import { Injectable } from '@nestjs/common';
import { InjectRepository, InjectEntityManager } from '@nestjs/typeorm';
import { Repository, EntityManager } from 'typeorm';
import { RrzArticles } from '../entity/rrz_articles.entity';

@Injectable()
export class AudioService {
  constructor(
    @InjectRepository(RrzArticles)
    private audioRepository: Repository<RrzArticles>,
    @InjectEntityManager()
    private readonly entityManager: EntityManager,
  ) {}

  async findAll(): Promise<RrzArticles[]> {
    return await this.audioRepository.find();
  }

  async findAudiosByCateId(id: any): Promise<RrzArticles[]> {
    return await this.audioRepository.find({
      select: [
        'id',
        'nodeId',
        'title',
        'img',
        'uptime',
        'pubtime',
        'viewCount',
        'addTime',
        'seoDescription',
        'sourceUrl',
      ],
      where: { nodeId: id },
    });
  }

  async findAudiosByCateIdWith(id: any): Promise<RrzArticles[]> {
    const qb = this.entityManager
      .createQueryBuilder(RrzArticles, 'article')
      .select([
        'article.id',
        'article.nodeId',
        'article.title',
        'article.img',
        'article.uptime',
        'article.pubtime',
        'article.viewCount',
        'article.seoDescription',
        'article.sourceUrl',
        'article.url',
        'article.duration',
        'node.name AS nodeName',
      ]) // 指定字段
      .leftJoinAndSelect('article.node', 'node')
      .where('article.nodeId = :nodeid', { nodeid: id });
    const articles = qb.getMany();
    return await articles;
  }
}
